package com.example.finalhomework

import android.util.Log
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.navigation.NavHostController
import com.example.finalhomework.ui.components.CardButton

@Composable
fun HomeScreen(navController: NavHostController) {
    Log.d("HomeScreen", "HomeScreen Composable executed")
    Box(
        modifier = Modifier
            .fillMaxSize()
    ) {
        Image(
            painter = painterResource(id = R.drawable.background10),
            contentDescription = null,
            modifier = Modifier
                .fillMaxWidth()
                .align(Alignment.TopCenter),
            contentScale = ContentScale.Crop
        )
        Column(
            modifier = Modifier
                .fillMaxSize()
                .padding(16.dp),
            verticalArrangement = Arrangement.Center,
            horizontalAlignment = Alignment.CenterHorizontally
        ) {
            Row(
                modifier = Modifier.fillMaxWidth(),
                horizontalArrangement = Arrangement.SpaceBetween
            ) {
                CardButton(
                    text = "Homework1",
                    imageResId = R.drawable.baseline_flag,
                    onClick = {
                        navController.navigate("manyhelloworld")
                        Log.d("HomeScreen", "Navigating to manyhelloworld")
                    },
                    modifier = Modifier
                        .size(width = 110.dp, height = 200.dp)
                        .padding(8.dp)
                )
    //            Spacer(modifier = Modifier.width(16.dp))
                CardButton(
                    text = "Homework2",
                    imageResId = R.drawable.baseline_list,
                    onClick = {
                        navController.navigate("secondhomework")
                        Log.d("HomeScreen", "Navigating to secondhomework")
                    },
                    modifier = Modifier
                        .size(width = 110.dp, height = 200.dp)
                        .padding(8.dp)
                )

                CardButton(
                    text = "Homework3",
                    imageResId = R.drawable.baseline_bright, // Replace with appropriate image
                    onClick = {
                        navController.navigate("thirdhomework")
                        Log.d("HomeScreen", "Navigating to thirdhomework")
                    },
                    modifier = Modifier
                        .size(width = 110.dp, height = 200.dp)
                        .padding(8.dp)
                )
            }
            Row(
                modifier = Modifier.fillMaxWidth(),
                horizontalArrangement = Arrangement.SpaceBetween
            ) {

                CardButton(
                    text = "CardGame",
                    imageResId = R.drawable.icons8_cardgame_30, // Replace with appropriate image
                    onClick = {
                        navController.navigate("cardgame")
                        Log.d("HomeScreen", "Navigating to cardgame")
                    },
                    modifier = Modifier
                        .size(width = 110.dp, height = 200.dp)
                        .padding(8.dp)
                )
                CardButton(
                    text = "MusicPlayer",
                    imageResId = R.drawable.baseline_library_music, // Replace with appropriate image
                    onClick = {
                        navController.navigate("musicplayer")
                        Log.d("HomeScreen", "Navigating to musicplayer")
                    },
                    modifier = Modifier
                        .size(width = 110.dp, height = 200.dp)
                        .padding(8.dp)
                )
                CardButton(
                    text = "Weather",
                    imageResId = R.drawable.baseline_cloud, // Replace with appropriate image
                    onClick = {
                        navController.navigate("weathercityscreen")
                        Log.d("HomeScreen", "Navigating to weathercityscreen")
                    },
                    modifier = Modifier
                        .size(width = 110.dp, height = 200.dp)
                        .padding(8.dp)
                )
            }
        }
        Image(
            painter = painterResource(id = R.drawable.background1),
            contentDescription = null,
            modifier = Modifier
                .fillMaxWidth()
                .align(Alignment.BottomCenter),
            contentScale = ContentScale.Crop
        )
    }
}

@Preview
@Composable
fun HomeScreenPreview() {
    HomeScreen(navController = NavHostController(LocalContext.current))
}